﻿using Dapper;
using Dapper_Core.Common;
using Dapper_Core.DAL.Impl;
using Dapper_Core.Model;
using Dapper_Core.Table;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;

namespace Dapper_Core.BLL.Impl
{
    public class Sys_DepartmentBusiness : BaseRepository<Sys_Department>
    {
        public Sys_DepartmentBusiness()
        {

        }

        public Sys_DepartmentBusiness(TransactionContext transactionContext) : base(transactionContext)
        {

        }

        public List<Sys_Department> GetDept1List(int? isYanfa = null, int? isHistory = 0, bool addTopItem=false)
        {
            try
            {
                string where = "where 1=1";
                DynamicParameters parameters = new DynamicParameters();
                if (isYanfa != null)
                {
                    where += " and IsYanfa=@isYanfa";
                    parameters.Add("isYanfa", isYanfa);
                }
                if (isHistory == 0 || isHistory == 1)
                {
                    where += " and IsHistory=@isHistory";
                    parameters.Add("isHistory", isHistory);
                }
                var anonymous = GetList(where, parameters).Select(a => new { Dept1Code = a.Dept1Code, Dept1Name = a.Dept1Name }).Distinct().ToList();
                List<Sys_Department> deptManagers = anonymous.Select(a => new Sys_Department { Dept1Code = a.Dept1Code, Dept1Name = a.Dept1Name }).ToList();
                if (addTopItem)
                {
                    deptManagers.Insert(0, new Sys_Department() { Dept1Code = "", Dept1Name = "--请选择--" });
                }
                return deptManagers;
            }
            catch (Exception ex)
            {
                NlogUtility.WriteError(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.FullName, "获取一级部门信息失败", "isYanfa=" + isYanfa + "，isHistory=" + isHistory + "，addTopItem=" + addTopItem, ex, "DapperCore");
                return new List<Sys_Department>();
            }
        }

        public List<Sys_Department> GetDept2List(string dept1Code, int? isYanfa = null, int? isHistory = 0, bool addTopItem = false)
        {
            try
            {
                string where = "where Dept1Code = @dept1Code";
                DynamicParameters parameters = new DynamicParameters();
                parameters.Add("dept1Code", dept1Code);
                if (isYanfa != null)
                {
                    where += " and IsYanfa=@isYanfa";
                    parameters.Add("isYanfa", isYanfa);
                }
                if (isHistory == 0 || isHistory == 1)
                {
                    where += " and IsHistory=@isHistory";
                    parameters.Add("isHistory", isHistory);
                }
                var anonymous = GetList(where, parameters).Select(a => new { Dept1Code = a.Dept1Code, Dept1Name = a.Dept1Name, Dept2Code = a.Dept2Code, Dept2Name = a.Dept2Name }).Distinct().ToList();
                List<Sys_Department> deptManagers = anonymous.Select(a => new Sys_Department { Dept1Code = a.Dept1Code, Dept1Name = a.Dept1Name, Dept2Code = a.Dept2Code, Dept2Name = a.Dept2Name }).ToList();
                if (addTopItem)
                {
                    deptManagers.Insert(0, new Sys_Department() { Dept2Code = "", Dept2Name = "--请选择--" });
                }
                return deptManagers;
            }
            catch (Exception ex)
            {
                NlogUtility.WriteError(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.FullName, "获取二级部门信息失败", "dept1Code="+ dept1Code + "，isYanfa=" + isYanfa + "，isHistory=" + isHistory + "，addTopItem=" + addTopItem, ex, "DapperCore");
                return new List<Sys_Department>();
            }
        }

        public List<Sys_Department> GetDept3List(string dept2Code, int? isYanfa = null, int? isHistory = 0, bool addTopItem = false)
        {
            try
            {
                string where = "where Dept2Code = @dept2Code";
                DynamicParameters parameters = new DynamicParameters();
                parameters.Add("dept2Code", dept2Code);
                if (isYanfa != null)
                {
                    where += " and IsYanfa=@isYanfa";
                    parameters.Add("isYanfa", isYanfa);
                }
                if (isHistory == 0 || isHistory == 1)
                {
                    where += " and IsHistory=@isHistory";
                    parameters.Add("isHistory", isHistory);
                }
                var anonymous = GetList(where, parameters).Select(a => new { Dept2Code = a.Dept2Code, Dept2Name = a.Dept2Name, Dept3Code = a.Dept3Code, Dept3Name = a.Dept3Name }).Distinct().ToList();
                List<Sys_Department> deptManagers = anonymous.Select(a => new Sys_Department { Dept2Code = a.Dept2Code, Dept2Name = a.Dept2Name, Dept3Code = a.Dept3Code, Dept3Name = a.Dept3Name }).ToList();
                if (addTopItem)
                {
                    deptManagers.Insert(0, new Sys_Department() { Dept3Code = "", Dept3Name = "--请选择--" });
                }
                return deptManagers;
            }
            catch (Exception ex)
            {
                NlogUtility.WriteError(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.FullName, "获取三级部门信息失败", "dept2Code=" + dept2Code + "，isYanfa =" + isYanfa + "，isHistory=" + isHistory + "，addTopItem=" + addTopItem, ex, "DapperCore");
                return new List<Sys_Department>();
            }
        }

        public Sys_Department GetModel(string dept1Code, string dept2Code, string dept3Code)
        {
            try
            {
                Expression<Func<Sys_Department, bool>> exp = department => department.Dept1Code == dept1Code && department.Dept2Code == dept2Code && department.Dept3Code == dept3Code;
                List<Sys_Department> list = base.GetList(exp, false, "OrderNo asc").ToList();
                if (list != null && list.Count > 0) return list[0];
                return null;
            }
            catch (Exception ex)
            {
                NlogUtility.WriteError(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.FullName, "获取部门信息实体失败", "dept1Code=" + dept1Code + "，dept2Code =" + dept2Code + "，dept3Code=" + dept3Code, ex, "DapperCore");
                return null;
            }
        }
    }
}
